package com.shoppeso.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;

import com.tencent.smtt.export.external.interfaces.SslError;
import com.tencent.smtt.export.external.interfaces.SslErrorHandler;
import com.tencent.smtt.sdk.CookieManager;
import com.tencent.smtt.sdk.CookieSyncManager;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

import java.util.HashMap;
import java.util.Map;


/**
 */
public class BaseWebView extends WebView {
    private Context mContext;

    public BaseWebView(Context context) {
        super(context);
        initView(context);
    }

    public BaseWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        initView(context);
    }

    public BaseWebView(Context context, AttributeSet attributeSet, int defStyle) {
        super(context, attributeSet, defStyle);
        initView(context);
    }


    private void initView(Context context) {
        mContext = context;
        getSettings().setJavaScriptEnabled(true);//JS
        requestFocus();
        getSettings().setDefaultTextEncodingName("utf-8");
        getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR);
        getSettings().setDatabaseEnabled(true);
        getSettings().setUseWideViewPort(true);
        getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        getSettings().setAppCacheEnabled(false);
        clearCache(true);
        CookieSyncManager.createInstance(mContext);
        CookieSyncManager.getInstance().startSync();
        CookieManager.getInstance().removeSessionCookie();
        String dir = context.getDir("database", Context.MODE_PRIVATE).getPath();
        //启用地理定位
        getSettings().setGeolocationEnabled(true);
        //设置定位的数据库路径
        getSettings().setGeolocationDatabasePath(dir);
        //最重要的方法，一定要设置，这就是出不来的主要原因
        getSettings().setDomStorageEnabled(true);
        setWebListener();
        setFocusableInTouchMode(true);
    }

    private void setWebListener() {


        setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                // TODO Auto-generated method stub
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            }


            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                loadByHead(url);
                return true;
            }

            @Override
            public void onReceivedError(WebView view, int errorCode,
                                        String description, String failingUrl) {

                super.onReceivedError(view, errorCode, description, failingUrl);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
            }
        });

    }

    public void loadByHead(String url) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("sprequest", "android");
        loadUrl(url, map);
    }

}